package com.chris.easymallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.chris.easymallproduct.dto.AttrDto;
import com.chris.easymallproduct.entity.ProductAttrValueEntity;
import com.chris.easymallproduct.service.ProductAttrValueService;
import com.chris.easymallproduct.vo.AttrInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chris.easymallproduct.entity.AttrEntity;
import com.chris.easymallproduct.service.AttrService;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.R;


/**
 * 商品属性
 *
 * @author chris
 * @email wysiwyg7@163.com
 * @date 2022-08-14 19:42:02
 */
@RestController
@RequestMapping("easymallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService attrValueService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 基础列表
     */
    @RequestMapping("/base/list/{id}")
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable("id") Long id) {
        PageUtils page = attrService.baseList(params, id);

        return R.ok().put("page", page);
    }

    /**
     * 基础列表
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R baseListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntities= attrValueService.baseListForSpu(spuId);

        return R.ok().put("data", productAttrValueEntities);
    }

    /**
     * 基础列表
     */
    @RequestMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> reqList) {
       attrValueService.updateSpuAttr(spuId,reqList);

        return R.ok();
    }



    /**
     * 销售属性列表
     */
    @RequestMapping("/sale/list/{id}")
    public R saleList(@RequestParam Map<String, Object> params, @PathVariable("id") Long id) {
        PageUtils page = attrService.saleList(params, id);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrInfoVo attr = attrService.getInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrDto dto) {
        attrService.save(dto);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrDto attr) {
        attrService.update(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
